import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/stores";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      redirect: "/home",
      meta: { title: "首页" },
      component: () => import("../views/Layout/index.vue"),
      children: [
        {
          path: "/home",
          name: "home",
          meta: { title: "首页" },
          component: () => import("../views/Home/index.vue"),
        },
        {
          path: "/article",
          name: "article",
          meta: { title: "健康百科" },
          component: () => import("../views/Article/index.vue"),
        },
        {
          path: "/notify",
          name: "notify",
          meta: { title: "消息中心" },
          component: () => import("../views/Notify/index.vue"),
        },
        {
          path: "/user",
          name: "user",
          meta: { title: "我的" },
          component: () => import("../views/User/index.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "login",
      meta: { title: "登录" },
      component: () => import("../views/Login/index.vue"),
    },
    {
      path: "/user/patient",
      component: () => import("@/views/User/patient.vue"),
      meta: { title: "家庭档案" },
    },
    {
      path: "/consult/fast",
      component: () => import("@/views/Consult/ConsultFast.vue"),
      meta: { title: "极速问诊" },
    },
    {
      path: "/consult/dep",
      component: () => import("@/views/Consult/ConsultDep.vue"),
      meta: { title: "选择科室" },
    },
    {
      path: "/consult/illness",
      component: () => import("@/views/Consult/ConsultIllness.vue"),
      meta: { title: "病情描述" },
    },
    {
      path: "/consult/pay",
      component: () => import("@/views/Consult/ConsultPay.vue"),
      meta: { title: "问诊支付" },
    },
    {
      path: "/user/consult",
      component: () => import("@/views/User/consult.vue"),
      meta: { title: "问诊记录" },
    },
    {
      path: "/room",
      component: () => import("@/views/Room/index.vue"),
      meta: { title: "问诊室" },
      beforeEnter(to) {
        if (to.query.payResult === "false") return "/user/consult";
      },
    },
    {
      path: "/user/cosultDetail/:id",
      component: () => import("@/views/User/cosultDetail.vue"),
      meta: { title: "问诊详情" },
    },
    {
      path: "/order/pay",
      component: () => import("@/views/Order/orderPay.vue"),
      meta: { title: "药品支付" },
    },
    {
      path: "/pay/result",
      component: () => import("../views/Order/orderPayResult.vue"),
      meta: { title: "药品支付结果" },
    },
    {
      path: "/orderDetail",
      component: () => import("../views/Order/orderDetail.vue"),
      meta: { title: "药品订单详情" },
    },
    {
      path: "/orderMap/:id",
      component: () => import("../views/Order/orderMap.vue"),
      meta: { title: "物流详情" },
    },
  ],
});
router.beforeEach((to) => {
  const store = useUserStore();
  const whiteList = ["/login"];
  if (!store.user?.token && !whiteList.includes(to.path)) return "/login";
});

router.afterEach((to) => {
  document.title = "优医问诊" + `-${to.meta.title}`;
});
export default router;
